﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TTQT;
using TTQT.TTQT_Code;

public partial class NhomHang : MasterControl //System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = string.Empty;
        if (!Page.IsPostBack)
        {
            bindGrid();
            visiblePanels(true, false);
            return;
        }
    }
    private void bindGrid()
    {
        try
        {
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select id,manhom,TenNhom,MaCN,case when gioitinh=0 then N'Đồ của nữ' else N'Đồ của nam' end as GioiTinh from NhomHang order by gioitinh";
            datagrid.DataSourceID = "ds";
            datagrid.DataBind();
        }
        catch (Exception)
        {
        }
    }
    private void visiblePanels(bool visibleGrid, bool visibleEdit)
    {
        this.pnGroupGrid.Visible = visibleGrid;
        this.pnEdit.Visible = visibleEdit;
    }
    protected void cmdAddNew_Click(object sender, EventArgs e)
    {
        lblMaNhomSua.Text = "";
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(false, true);
        this.txtId.Text = string.Empty;
        this.txtMaNhom.Text = string.Empty;
        this.txtTenNhom.Text = string.Empty;
    }

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            // Nếu là sửa Thì ID sẽ khác rỗng
            if (txtId.Text != "")
            {
                if (!txtMaNhom.Text.Trim().ToLower().Equals(lblMaNhomSua.Text.Trim().ToLower()))
                {
                    try
                    {
                        ds.SelectCommand = "select manhom from NhomHang where manhom='" + txtMaNhom.Text + "' and macn='" + UserInfo.MaKho + "'";
                        ds.SelectCommandType = SqlDataSourceCommandType.Text;
                        DataView hehe = (DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty);
                        if (hehe.Count > 0)
                        {
                            MyWebUtils.MyWebUtil.alert(this.Page, "Ma nhom nay da ton tai. Ban vui long nhap lai!");
                            return;
                        }
                       
                    }
                    catch (Exception exx)
                    {
                        MyWebUtils.MyWebUtil.alert(this.Page, "Ma nhom nay da ton tai.");
                        return;
                    }

                }
                ds.UpdateCommand = "update NhomHang set [MaNhom]=N'" + txtMaNhom.Text + "', TenNhom=N'" + txtTenNhom.Text + "', gioitinh=" + cboGioiTinh.SelectedValue + " where id=" + txtId.Text;
                ds.Update();
                bindGrid();
                visiblePanels(true, false);
                lblMaNhomSua.Text = "";
            }
            // Nếu là thêm mới thì ID sẽ rỗng
            else
            {
                ds.InsertCommand = "insert into NhomHang([MaNhom],TenNhom,MaCN,GioiTinh) Values (N'" + txtMaNhom.Text + "',N'" + txtTenNhom.Text + "','" + UserInfo.Brn_Code + "'," + cboGioiTinh.SelectedValue + ")";
                ds.Insert();
                bindGrid();
                visiblePanels(true, false);
            }
        }
        catch (Exception ex)
        {
            lblUpdateStatus.Attributes.Add("style", "color:red;font-weight:bold;");
            this.lblUpdateStatus.Text = ex.ToString();
            //Log methods here
        }
    }
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        lblMaNhomSua.Text = "";
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(true, false);
    }
    protected void datagrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "cmdEdit")
        {
            visiblePanels(false, true);
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select * from NhomHang where id=" + e.Item.Cells[0].Text;
            DataTable hehe = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();

            hehe.Rows[0][1].ToString();
            this.txtId.Text = hehe.Rows[0][0].ToString();
            this.txtMaNhom.Text = hehe.Rows[0][1].ToString();
            this.lblMaNhomSua.Text = hehe.Rows[0][1].ToString();
            this.txtTenNhom.Text = hehe.Rows[0][2].ToString();
            this.cboGioiTinh.SelectedValue = hehe.Rows[0][4].ToString();
            // Save old info
        }
        else if (e.CommandName == "cmdDelete")
        {
            ds.DeleteCommand = "delete NhomHang where id=" + e.Item.Cells[0].Text;
            ds.Delete();
            bindGrid();
        }
    }
    protected void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        Button cmdDel = (Button)e.Item.FindControl("cmdDelete");
        if (cmdDel != null) cmdDel.Attributes.Add("onclick", TTQTCommon.delete_Confirm);
    }
    protected void datagrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        datagrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid();
    }
}
